package com.aix.android.framework.dialog;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.Window;

import com.aix.android.framework.Constants;
import com.aix.android.framework.R;
import com.aix.android.framework.util.ImageUtils;

public class AcquirePictureDialog {
	
	private Activity mActivity;
	
	private String filename;
	
	private int requestCodeForCamera;
	
	private int requestCodeForAlbum;
	
	private AcquirePictureDialog(Activity activity, int requestCodeForCamera, int requestCodeForAlbum) {
		mActivity = activity;
		this.requestCodeForCamera = requestCodeForCamera;
		this.requestCodeForAlbum = requestCodeForAlbum;
	}
	
	public static AcquirePictureDialog open(Activity activity, int requestCodeForCamera, int requestCodeForAlbum) {
		final AcquirePictureDialog acquirePictureDialog = 
				new AcquirePictureDialog(activity, requestCodeForCamera, requestCodeForAlbum);
		
		final AlertDialog dialog = new AlertDialog.Builder(activity).create();
		
		dialog.show();
		Window window = dialog.getWindow();
		window.setContentView(R.layout.dialog_menus);
		window.setGravity(Gravity.BOTTOM);
		View viewCancle = (View) window.findViewById(R.id.relativeLayoutCancle);
		viewCancle.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				//关闭对话框
				dialog.cancel();
			}
		});
		
		View viewCamera = (View) window.findViewById(R.id.relativeLayoutCamera);
		viewCamera.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				dialog.cancel();
				//打开相机
				Intent target = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				
				// 生成文件名
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				acquirePictureDialog.filename = sdf.format(new Date()) + ".jpg";
				File file = new File(Constants.CAMERA_PHOTO_DIR, acquirePictureDialog.filename);
				Uri uri = Uri.fromFile(file);
				target.putExtra(MediaStore.EXTRA_OUTPUT, uri);
				
				acquirePictureDialog.mActivity.startActivityForResult(target, acquirePictureDialog.requestCodeForCamera);
			}
		});
		
		View viewAlbum = (View) window.findViewById(R.id.relativeLayoutAlbum);
		viewAlbum.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
				
				//打开相册
				Intent target = new Intent(Intent.ACTION_PICK);
				target.setType("image/*");
				
				acquirePictureDialog.mActivity.startActivityForResult(target, acquirePictureDialog.requestCodeForAlbum);
			}
		});
		
		return acquirePictureDialog;
	}
	
	public File getFile(int requestCode, Intent data) {
		File file = null;
		
		if (requestCode == this.requestCodeForAlbum) {
			ContentResolver resolver = mActivity.getContentResolver();
			Uri originalUri = data.getData();
				
			file = new File(ImageUtils.getAlbumImgPath(resolver,originalUri));
		} else if (requestCode == this.requestCodeForCamera) {
			file = new File(Constants.CAMERA_PHOTO_DIR, this.filename);
		}
		
		return file;
	}
	
	/*
	 * 当使用相机拍摄图片时，返回生成的文件名
	 */
	public String getFileName() {
		return this.filename;
	}
	
	/*
	 * 获取文件的路径，例如：/sdcard/temp/20140111.jpg
	 */
	public String getFilePath() {
		return Constants.CAMERA_PHOTO_DIR + this.filename;
	}
	
	public Bitmap getBitmapByWidth(int requestCode, Intent data, int width) {
		Bitmap bitmap = null;
		String filename = null;
		
		if (requestCode == this.requestCodeForAlbum) {
			Uri originalUri = data.getData();
				
//			try {
//				bitmap = MediaStore.Images.Media.getBitmap(mActivity.getContentResolver(), originalUri);
//				
//				return Bitmap.createScaledBitmap(bitmap, width, width * bitmap.getHeight() / bitmap.getWidth(), false);
//			} catch (FileNotFoundException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
			
			filename = ImageUtils.getAlbumImgPath(mActivity.getContentResolver(), originalUri);
		} else if (requestCode == this.requestCodeForCamera) {
			filename = Constants.CAMERA_PHOTO_DIR + this.filename;
		}
		
		BitmapFactory.Options options = new Options();
		options.inJustDecodeBounds = true;
		
		BitmapFactory.decodeFile(filename, options);
		options.inSampleSize = ImageUtils.calculateInSampleSize(options, width, width * options.outHeight / options.outWidth);
		options.inJustDecodeBounds = false;
		bitmap = BitmapFactory.decodeFile(filename, options);
		
		return bitmap;
	}
	
	/*
	 * out of memory error may occur
	 */
	public Bitmap getBitmap(int requestCode, Intent data) {
		Bitmap bitmap = null;
		String filename = null;
		
		if (requestCode == this.requestCodeForAlbum) {
			Uri originalUri = data.getData();
			
//			try {
//				bitmap = MediaStore.Images.Media.getBitmap(mActivity.getContentResolver(), originalUri);
//				
//				return Bitmap.createScaledBitmap(bitmap, width, width * bitmap.getHeight() / bitmap.getWidth(), false);
//			} catch (FileNotFoundException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
			
			filename = ImageUtils.getAlbumImgPath(mActivity.getContentResolver(), originalUri);
		} else if (requestCode == this.requestCodeForCamera) {
			filename = Constants.CAMERA_PHOTO_DIR + this.filename;
		}
		
		bitmap = BitmapFactory.decodeFile(filename);
		
		return bitmap;
	}
}
